package com.vincent.kdp.Activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.vincent.kdp.PostUrl;
import com.vincent.kdp.R;
import com.vincent.kdp.bean.ApiResponse;
import com.vincent.kdp.ui.other.MainActivity;
import com.vincent.kdp.util.BaseParam;
import com.vincent.kdp.util.GsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


public class EmployeeLoginActivity extends Activity {

    private Bundle bundle;
    private int departId;
    private RequestQueue requestQueue;//请求的队列

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jihuo);
        requestQueue= Volley.newRequestQueue(this);
        Bundle bundle = getIntent().getExtras();
        departId= Integer.parseInt(bundle.getString("departId"));

        //调用激活此设备的方法
        dialog();
    }

    /**
     * 激活设备
     */
    private void openFacility() {
        RequestParams params = new RequestParams (PostUrl.OPEN_FACILITY);
        params.addBodyParameter("number", BaseParam.getDeviceId());
        params.addBodyParameter("departId", departId+"");
        x.http().post(params,new Callback.CommonCallback<String>(){
            @Override
            public void onSuccess(String result) {
                Gson gson= GsonUtils.build();
                ApiResponse response=gson.fromJson(result, ApiResponse.class);
                if(response.isSuccess()){
                    Toast.makeText(EmployeeLoginActivity.this, "设备ID:"+BaseParam.getDeviceId()+"激活成功！", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(EmployeeLoginActivity.this, MainActivity.class));
                    finish();
                    ShangjiaActivity.instance.finish();
                    BumenActivity.instance.finish();
                }else{
                    Toast.makeText(EmployeeLoginActivity.this, "激活失败！", Toast.LENGTH_SHORT).show();
                    finish();
                }
           }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(EmployeeLoginActivity.this,"系统错误,联系客服",Toast.LENGTH_SHORT);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    protected void dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(EmployeeLoginActivity.this);
        builder.setMessage("是否要激活此设备?");
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                openFacility();
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                EmployeeLoginActivity.this.finish();
            }
        });

        builder.setCancelable(false);

        builder.create().show();

    }




}
